﻿using BySoft.Server.Business;
using BySoft.Server.Common.Entity;
using BySoft.Server.Common.Enum;
using BySoft.Server.Consts;
using BySoft.Web.Utility;
using Microsoft.Office.Interop.Excel;
using OneSoft.EntityCore;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace BySoft.Web.Site.MasteData
{
    public partial class StockAdd : AuthBasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                InitDDL();
               
            }
            Master.ValidationGroup = "gSubmit";
        }

        private void InitDDL()
        {
            BindParameterForListControl("StockType", ddlStockType);
            //UIHelper.InsertFirstItem(ddlStockType);
        }



        //public static void InitStockTypeList(DropDownList ddl, bool insertNullValue)
        //{
        //    System.Data.DataTable dt = new System.Data.DataTable();
        //    dt.Columns.Add("id");
        //    dt.Columns.Add("mc");

        //    DataRow row = dt.NewRow();
        //    row["id"] = "1";
        //    row["mc"] = "原料";
        //    dt.Rows.Add(row);

        //    row = dt.NewRow();
        //    row["id"] = "2";
        //    row["mc"] = "半成品";
        //    dt.Rows.Add(row);

        //    row = dt.NewRow();
        //    row["id"] = "3";
        //    row["mc"] = "成品";
        //    dt.Rows.Add(row);
          
        //    row = dt.NewRow();
        //    row["id"] = "5";
        //    row["mc"] = "刀具";
        //    dt.Rows.Add(row);

        //    if (insertNullValue)
        //    {
        //        row = dt.NewRow();
        //        row["id"] = "";
        //        row["mc"] = "请选择...";
        //        dt.Rows.InsertAt(row, 0);
        //    }

        //    ddl.DataSource = dt;
        //    ddl.DataValueField = "id";
        //    ddl.DataTextField = "mc";
        //    ddl.DataBind();
        //}
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            try
            {
                if (string.IsNullOrEmpty(CurrentUser.UserID))
                {
                    UIHelper.AlertMessage(this.Page, "等待时间太久，请重新填写仓库类别资料！");
                    return;
                }

                if (!string.IsNullOrEmpty(this.txtStockName.Text.Trim()))
                {
                    if ((new MdStock()).IsNameExists(this.txtStockName.Text.Trim(),""))
                    {
                        UIHelper.AlertMessage(this.Page, "仓库类别名称已存在，请重新填写！");
                        return;
                    }
                    MdStockEntity entity = new MdStockEntity();
                    entity.StockID = GetString(txtStockID);

                    entity.StockName = GetString(txtStockName);
                    entity.Status = "1";
                    entity.CreateDate = DateTime.Now;
                    entity.CreateUserID = CurrentUser.UserID;
                    entity.CreateUserName = CurrentUser.UserName;

                    if (MdStock.InsertMdStock(entity))
                    {
                        base.LogAction(BaseEnum.ActionType.Add, "", true, null, new EntityBase[] { entity });
                        UIHelper.AlertMessageGoToURL(this.Page, Message.SaveSuccess, "../MasteData/StockList.aspx");
                    }
                    else
                    {
                        UIHelper.AlertMessage(this.Page, Message.SaveFail);
                    }
                }

            }
            catch (Exception ex)
            {
                UIHelper.AlertMessage(this.Page, FomatString(ex.Message.ToString()));
            }
        }

       
    }
}